﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;

namespace PivotViewerSL
{
    public partial class MainPage : UserControl
    {
        ClientContext ctx;
        List lst;
        ListItemCollection lc;
        List<SPItemSource> lstIS;
        private string _webUrl;
        public MainPage()
        {
            _webUrl=((App)App.Current).WebUrl;
            lstIS = new List<SPItemSource>();
            InitializeComponent();
            
            
            //ctx = ClientContext.Current;
            ctx = new ClientContext(_webUrl);
            //ctx.Load(ctx.Site);
            //ctx.Load(ctx.Web);
            lst=ctx.Web.Lists.GetByTitle(((App)App.Current).ListName);
            ctx.Load(lst);
            lc = lst.GetItems(CamlQuery.CreateAllItemsQuery());
            ctx.Load(lc, l => l.Include(i => i.FieldValuesAsText, i => i.File, i => i["Created"], i => i["File_x0020_Type"], i => i["Title"], i => i["Author"], i => i["Modified"], i => i["File_x0020_Size"]));
            ClientRequestSucceededEventHandler crseh = new ClientRequestSucceededEventHandler(lcLoad);
            ClientRequestFailedEventHandler crfeh = new ClientRequestFailedEventHandler(lcFailed);
            ctx.ExecuteQueryAsync(crseh, lcFailed);
        
        }
        private void lcFailed(object sender, ClientRequestFailedEventArgs crfea)
        {
            object error= crfea.ErrorDetails;
        }
        private void lcLoad(object sender, ClientRequestSucceededEventArgs crsea)
        {
            ctx = new ClientContext(_webUrl);;
            
            foreach (ListItem li in lc)
            { 
                SPItemSource sis = new SPItemSource();
                try
                {
                    string title = li.FieldValuesAsText["Title"];
                    if (string.IsNullOrEmpty(title.Trim()))
                        title = li.File.Name;
                    if (!(li.File.Name.ToLower().EndsWith("docx") || li.File.Name.ToLower().EndsWith("pptx") || li.File.Name.ToLower().EndsWith("ppt")))
                        continue;
                    sis.Title = title;
                    sis.FileType = li.FieldValuesAsText["File_x0020_Type"];
                    sis.Created = ((DateTime)(li["Created"]));
                    sis.Author = FieldUserValue.FromUser(li.FieldValuesAsText["Author"]).LookupValue;
                    sis.Modified = ((DateTime)(li["Modified"]));
                    sis.FileSize = int.Parse(li.FieldValuesAsText["File_x0020_Size"].ToString());
                    
                    sis.File = li.File;
                    
                    
                   
                }
                catch (Exception ex)
                {
                    continue;
                }
                lstIS.Add(sis);
            }
            this.Dispatcher.BeginInvoke(BindPV);
        }
        private void BindPV()
        {
            
            this.pv4SP2.ItemsSource = lstIS;
            
            this.pv4SP2.UpdateLayout();
            
        }

    }
}
